﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.EntityModel;
using YasharEl.Infrastructure.EntityModel.Specifications.QueryableSpecification;

namespace YasharEl.Infrastructure.TaskEngine.PersistenceModel.Specifications
{
    public class TaskQueryableSpecification : QueryableSpecification<ITask, long>, ITaskSpecification
    {
        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public TaskQueryableSpecification(IUnitOfWorkConvertor unitOfWorkConvertor)
            : base(unitOfWorkConvertor)
        {
        }

        #endregion

        #region Implementation of ITaskSpecification

        /// <summary>
        /// Specifies the userId the query will filter entities for.
        /// </summary>
        public ITaskSpecification AssignedTo(long userId)
        {
            Queryable.Where(t => t.CurrentAssignments.Any(a => a.AssignedTo.UserId == userId));

            return this;
        }

        /// <summary>
        /// Specifies the name the query will filter entities for.
        /// </summary>
        public ITaskSpecification WithName(string name)
        {
            Queryable = Queryable.Where(c => c.TaskName == name);
            return this;
        }

        public ITaskSpecification RelatedToInitiator(Guid initiatorCorrelator)
        {
            Queryable = Queryable.Where(c => c.InitiatorCorrelationId == initiatorCorrelator);
            return this;
        }

        #endregion
    }
}
